package com.ruoyi.bantong.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.bantong.domain.BtUserWithdraw;
import com.ruoyi.bantong.mapper.BtUserMapper;
import com.ruoyi.bantong.service.IBtUserAccountRecordService;
import com.ruoyi.common.core.domain.entity.BtUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.mapper.BtUserWithdrawMapper;
import com.ruoyi.bantong.service.IBtUserWithdrawService;



/**
 * 用户提现记录Service业务层处理
 * 
 * @author laohei
 * @date 2021-02-04
 */
@Service
public class BtUserWithdrawServiceImpl implements IBtUserWithdrawService 
{
    @Autowired
    private BtUserWithdrawMapper btUserWithdrawMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private BtUserMapper userMapper;

    @Autowired
    private IBtUserAccountRecordService accountRecordService;

    /**
     * 查询用户提现记录
     * 
     * @param uwId 用户提现记录ID
     * @return 用户提现记录
     */
    @Override
    public BtUserWithdraw selectBtUserWithdrawById(Integer uwId)
    {
        return btUserWithdrawMapper.selectBtUserWithdrawById(uwId);
    }

    /**
     * 查询用户提现记录列表
     * 
     * @param btUserWithdraw 用户提现记录
     * @return 用户提现记录
     */
    @Override
    public List<BtUserWithdraw> selectBtUserWithdrawList(BtUserWithdraw btUserWithdraw)
    {
        return btUserWithdrawMapper.selectBtUserWithdrawList(btUserWithdraw);
    }

    /**
     * 新增用户提现记录
     * 
     * @param btUserWithdraw 用户提现记录
     * @return 结果
     */
    @Override
    public int insertBtUserWithdraw(BtUserWithdraw btUserWithdraw) {
        int i = 0;
        SysConfig sysConfig = sysConfigMapper.checkConfigKeyUnique("bt.withdraw.service");
        SysConfig sysConfig1 = sysConfigMapper.checkConfigKeyUnique("bt.withdraw.lowest");
        BigDecimal servicePer = new BigDecimal(sysConfig.getConfigValue()); // 服务费百分比
        BigDecimal lowestFee = new BigDecimal(sysConfig1.getConfigValue()); // 最小提现额度
        BigDecimal withdrawBean = btUserWithdraw.getWithdrawBean();//本次提现金额
        if(lowestFee.compareTo(withdrawBean) == 1){
            throw new CustomException("提现金额不能小于"+lowestFee.toString());
        }
        BtUser btUser = userMapper.selectBtUserById(btUserWithdraw.getUserId());
        BigDecimal btBean = btUser.getBtBean();//余额
        if(lowestFee.compareTo(btBean) == 1){
            throw new CustomException("余额不足");
        }
        BigDecimal serviceFee = withdrawBean.multiply(servicePer).divide(BigDecimal.valueOf(100));//服务费
        BigDecimal beanFee = withdrawBean.subtract(serviceFee); //实际到账
        btUserWithdraw.setServiceFee(serviceFee);
        btUserWithdraw.setPracticalBean(beanFee);
        btUserWithdraw.setCreateTime(DateUtils.getNowDate());
        try {
            i = btUserWithdrawMapper.insertBtUserWithdraw(btUserWithdraw);
            //提现
            accountRecordService.updataUserAccount(btUserWithdraw.getUserId(), withdrawBean,BigDecimal.ZERO,btUserWithdraw.getOsid(),
                    "bt_user_withdraw","out",11);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 修改用户提现记录
     * 
     * @param btUserWithdraw 用户提现记录
     * @return 结果
     */
    @Override
    public int updateBtUserWithdraw(BtUserWithdraw btUserWithdraw)
    {
        BtUserWithdraw withdraw = btUserWithdrawMapper.selectBtUserWithdrawById(btUserWithdraw.getUwId());
        Integer status = btUserWithdraw.getStatus();
        if(status == 2){ //驳回
            //提现
            try {
                accountRecordService.updataUserAccount(withdraw.getUserId(),withdraw.getWithdrawBean(),BigDecimal.ZERO,btUserWithdraw.getUwId(),
                        "bt_user_withdraw","in",12);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        btUserWithdraw.setUpdateTime(DateUtils.getNowDate());
        return btUserWithdrawMapper.updateBtUserWithdraw(btUserWithdraw);
    }

    /**
     * 批量删除用户提现记录
     * 
     * @param uwIds 需要删除的用户提现记录ID
     * @return 结果
     */
    @Override
    public int deleteBtUserWithdrawByIds(Integer[] uwIds)
    {
        return btUserWithdrawMapper.deleteBtUserWithdrawByIds(uwIds);
    }

    /**
     * 删除用户提现记录信息
     * 
     * @param uwId 用户提现记录ID
     * @return 结果
     */
    @Override
    public int deleteBtUserWithdrawById(Integer uwId)
    {
        return btUserWithdrawMapper.deleteBtUserWithdrawById(uwId);
    }
}
